package com.lhs.aopcache.processor;

import com.lhs.aopcache.base.CacheMetadata;
import com.lhs.aopcache.exception.AopCacheException;
import com.lhs.aopcache.util.StringUtil;
import org.aopalliance.intercept.MethodInvocation;

public class AopCacheRemoveProcessor extends AbstractAopCacheProcessor {

    @Override
    public void postProcess(CacheMetadata metadata,
                            MethodInvocation invocation, Object result) {
        try {
            String group = metadata.getGroup();
            String removeKeys = metadata.getRemoveKeys();
            String removeGroups = metadata.getRemoveGroups();
            if (!StringUtil.isBlank(removeKeys)) {
                String[] removeArray = removeKeys.split(SPLIT_KEY);
                for (String removeStr : removeArray) {
                    getAopCache().remove(group, removeStr);
                }
            }
            if (!StringUtil.isBlank(removeGroups)) {
                String[] removeGroupArray = removeGroups.split(SPLIT_KEY);
                for (String removeGroupStr : removeGroupArray) {
                    getAopCache().cleanGroup(removeGroupStr);
                }
            }

        } catch (Throwable e) {
            throw new AopCacheException(e);
        }
    }
}
